package com.haier.spark.project.conf

import java.util.Properties

import com.haier.spark.project.constant.Constants
import org.apache.log4j.Logger

class ConfigurationManager private {

}

object ConfigurationManager {
  private val logger = Logger.getLogger(getClass)
  private val properties: Properties = getProperties(Constants.MY_PROPERTIES_PATH)

  /**
    * 获取加载的properties配置文件
    *
    * @param filepath 文件路径
    * @return
    */
  def getProperties(filepath: String): Properties = {
    val in = ConfigurationManager.getClass.getClassLoader.getResourceAsStream(filepath)
    val properties = new Properties()
    properties.load(in)
    properties
  }

  /**
    * 根据key获取value
    *
    * @param key
    * @return
    */
  def getValue(key: String): String = properties.getProperty(key)

  /**
    * 根据key获取int类型的value
    *
    * @param key
    * @return
    */
  def getInt(key: String): Integer = {
    val value = getValue(key)
    var result = 0
    try
      result = value.toInt
    catch {
      case e: Exception =>
        logger.error("value:[{}]转换数字失败，返回0", value, e)
    }
    result
  }

  /**
    * 根据key获取Boolean类型的value
    *
    * @param key
    * @return
    */
  def getBoolean(key: String): Boolean = {
    val value = getValue(key)
    return value.toBoolean
  }
}
